<template>
<div class="searchTop">
    <svg class="icon" aria-hidden="true" @click="$router.go(-1)">
      <use xlink:href="#icon-fanhuijiantou"></use>
    </svg>
    <input
      type="text"
      placeholder="陈奕迅"
      v-model="searchKey"
      @keydown.enter="enterKey"
    />
  </div>
  <div class="history">
    <span class="searchSpan">历史</span>
    <span v-for="item in keyWordList" :key="item" class="searchKey" @click="searchHistory(item)">{{item}}</span>
    <svg class="icon" aria-hidden="true" @click="delHistory">
      <use xlink:href="#icon-shanchu"></use>
    </svg>
  </div>
  <div class="itemList">
      <div class="item" v-for="(item, i) in searchList" :key="i">
        <div class="itemLeft" @click="updateplayIndex(item)">
          <span class="leftSpan">{{ i + 1 }}</span>
          <div>
            <p>{{ item.name }}</p>
            <span v-for="(item1, index) in item.artists" :key="index">{{
              item1.name
            }}</span>
          </div>
        </div>
        <div class="itemRight">
          <svg class="icon bofang" aria-hidden="true" v-if='item.mvid !=0'>
            <use xlink:href="#icon-24gl-playCircle"></use>
          </svg>
          <svg class="icon liebiao" aria-hidden="true">
            <use xlink:href="#icon-list"></use>
          </svg>
        </div>
      </div>
    </div>
    <itemMusicFooter/>
</template>

<script>
import { getSearchMusic } from "@/request/api/home.js";
import itemMusicFooter from '@/components/item/itemMusicFooter.vue'
    export default {

        data(){
           return {
            keyWordList:[],
            searchKey:"",
            searchList:[]
           }
        },
        mounted(){
                this.keyWordList=JSON.parse(localStorage.getItem('keyWordList')) ? JSON.parse(localStorage.getItem("keyWordList"))
      : [];
        },
        methods:{
       
            enterKey: async function(){
              if (this.searchKey !== "") {
                this.keyWordList.unshift(this.searchKey)
                // 去重
                this.keyWordList=[...new Set(this.keyWordList)]
                // 固定长度
                if(this.keyWordList.length>5){
                    this.keyWordList.splice(this.keyWordList.length-1)
                }
                localStorage.setItem("keyWordList",JSON.stringify(this.keyWordList))
                let res =await getSearchMusic(this.searchKey)
                console.log(res)
                this.searchList=res.data.result.songs
                this.searchKey=""
            }
          },
            delHistory(){
                localStorage.removeItem("keyWordList")
                this.keyWordList=""
            },
            searchHistory:async function(item){
                let res =await getSearchMusic(item)
                console.log(res);
                this.searchList=res.data.result.songs
            },
            updateplayIndex(item){
              item.al=item.album
              item.al.picUrl=item.album.artist.img1v1Url
              this.$store.commit('pushIndex',item)
              this.$store.commit('updateIndex',this.$store.state.playList.length-1)
            }
        }
    }
</script>

<style lang="less" scoped>
.searchTop {
  width: 100%;
  height: 1rem;
  padding: 0 0.2rem;
  display: flex;
  align-items: center;
  input {
    margin-left: 0.2rem;
    border: none;
    border-bottom: 1px solid #999;
    width: 90%;
    padding: 0.1rem;
  }
}
.history {
  width: 100%;
  padding: 0.2rem;
  position: relative;
  .searchSpan {
    font-weight: 700;
  }
  .searchKey {
    padding: 0.1rem 0.2rem;
    background-color: rgb(185, 169, 169);
    border-radius: 0.4rem;
    margin: 0.1rem 0.2rem;
    display: inline-block;
  }
  .icon {
    width: 0.3rem;
    height: 0.3rem;
    position: absolute;
    right: 0.2rem;
  }
}
.itemList {
    width: 100%;
    padding: .2rem;
    .item {
      width: 100%;
      height: 1.4rem;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .itemLeft {
        width: 85%;
        height: 100%;
        display: flex;
        align-items: center;
        .leftSpan {
          display: inline-block;
          width: 0.2rem;
          text-align: center;
        }
        div {
          p {
            width: 4.54rem;
            height: .4rem;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            font-weight: 700;
          }
          span{
            font-weight: 400;
            font-size: .24rem;
            color: #999;
          }
          margin-left: 0.3rem;
        }
      }
      .itemRight{
        width: 20%;
        height: 100%;
        display: flex;
        // justify-content: space-between;
        align-items: center;
        position: relative;
        .icon{
          fill: #999;
        }
         .bofang{
            position: absolute;
            left: 0;
          }
         .liebiao{
            position: absolute;
            right: 0;
          }
      }
    }
  }
</style>